package com.ruoyi.teaching.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.CourseDesignMapper;
import com.ruoyi.teaching.domain.CourseDesign;
import com.ruoyi.teaching.service.ICourseDesignService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程设计Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-03
 */
@Service
@Slf4j
public class CourseDesignServiceImpl implements ICourseDesignService
{
    @Autowired
    private CourseDesignMapper courseDesignMapper;

    /**
     * 查询课程设计
     *
     * @param id 课程设计主键
     * @return 课程设计
     */
    @Override
    public CourseDesign selectCourseDesignById(Long id)
    {
        return courseDesignMapper.selectCourseDesignById(id);
    }

    /**
     * 查询课程设计列表
     *
     * @param courseDesign 课程设计
     * @return 课程设计
     */
    @Override
    public List<CourseDesign> selectCourseDesignList(CourseDesign courseDesign)
    {
        return courseDesignMapper.selectCourseDesignList(courseDesign);
    }

    /**
     * 新增课程设计
     *
     * @param courseDesign 课程设计
     * @return 结果
     */
    @Override
    public int insertCourseDesign(CourseDesign courseDesign)
    {
        courseDesign.setCreateTime(DateUtils.getNowDate());
        return courseDesignMapper.insertCourseDesign(courseDesign);
    }

    /**
     * 修改课程设计
     *
     * @param courseDesign 课程设计
     * @return 结果
     */
    @Override
    public int updateCourseDesign(CourseDesign courseDesign)
    {
        courseDesign.setUpdateTime(DateUtils.getNowDate());
        return courseDesignMapper.updateCourseDesign(courseDesign);
    }

    /**
     * 批量删除课程设计
     *
     * @param ids 需要删除的课程设计主键
     * @return 结果
     */
    @Override
    public int deleteCourseDesignByIds(Long[] ids)
    {
        return courseDesignMapper.deleteCourseDesignByIds(ids);
    }

    /**
     * 删除课程设计信息
     *
     * @param id 课程设计主键
     * @return 结果
     */
    @Override
    public int deleteCourseDesignById(Long id)
    {
        return courseDesignMapper.deleteCourseDesignById(id);
    }

    @Override
    public List<CourseDesign> selectCourseDesignByStageId(Long id) {
        return courseDesignMapper.selectList(new LambdaQueryWrapper<CourseDesign>().eq(CourseDesign::getStage,id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCourseDesignByStage(Integer stage, List<CourseDesign> courseDesignList) {
        //1.根据已有的课程id去删除需要删除课程设计
        List<Long> idList = courseDesignList.stream().filter(courseDesign -> courseDesign.getId() != null).map(courseDesign -> courseDesign.getId()).collect(Collectors.toList());
        courseDesignMapper.deleteCourseDesignByNotInIds(idList);
        //2.遍历courseDesignList，判断是更新 或者 新增课程设计
        //如果有id就修改，没有id就新增
        for (CourseDesign courseDesign : courseDesignList) {
            if(courseDesign.getId() != null){
                courseDesignMapper.updateCourseDesign(courseDesign);
            }else{
                courseDesign.setCreateBy(SecurityUtils.getUsername());
                courseDesign.setCreateTime(new Date());
                courseDesignMapper.insertCourseDesign(courseDesign);
            }
        }
        return 1;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public  int updateCourseDesignRelatedInterview(Integer cdId, List<Long> interviewIds) {
        Long interviewId = null;
        //根据课程设计id删除原有关联面试题
        //courseDesignMapper.deleteCourseDesignInterview(cdId);
        //遍历interviewIds,批量关联对应课程面试题
        for (int i = 0; i < interviewIds.size(); i++) {
            interviewId = interviewIds.get(i);
            try {
                courseDesignMapper.insertCourseDesignInterview(cdId, interviewId);
            } catch (Exception e) {
                log.warn("该面试题:{}已经关联过课程:{}",interviewId,cdId);
            }
        }
        return 1;
    }
}
